#version 460 core

layout (location = 0) out vec4 FragColor;
layout (location = 1) out vec4 BrightColor;  

uniform vec4 lightPos;
uniform float time;
uniform float prevTime;
uniform vec3 noiseScale;
uniform vec4 lightAmbient;
uniform vec4 lightDiffuse;              
uniform vec4 lightSpecular;  
uniform vec3 viewPos;
uniform vec4 materialAmbient;           
uniform vec4 materialDiffuse;           
uniform vec4 materialSpecular;          
uniform float materialShininess;
uniform sampler2D map0;


in vec3 FragPos;
in vec3 Normal;
in vec2 texCoord0;

//out vec4 fragColor;

//noise code from https://www.shadertoy.com/view/4lfSzS
vec4 hash4( vec4 n ) { return fract(sin(n)*1399763.5453123); }
vec3 hash3( vec3 n ) { return fract(sin(n)*1399763.5453123); }
vec3 hpos( vec3 n ) { return hash3(vec3(dot(n,vec3(157.0,113.0,271.0)),dot(n,vec3(271.0,157.0,113.0)),dot(n,vec3(113.0,271.0,157.0)))); }
//vec4 hash4( vec4 n ) { return fract(n*fract(n*0.5453123)); }
//vec4 hash4( vec4 n ) { n*=1.987654321; return fract(n*fract(n)); }
float noise4q(vec4 x)
{
	vec4 n3 = vec4(0,0.25,0.5,0.75);
	vec4 p2 = floor(x.wwww+n3);
	vec4 b = floor(x.xxxx+n3) + floor(x.yyyy+n3)*157.0 + floor(x.zzzz +n3)*113.0;
	vec4 p1 = b + fract(p2*0.00390625)*vec4(164352.0, -164352.0, 163840.0, -163840.0);
	p2 = b + fract((p2+1.0)*0.00390625)*vec4(164352.0, -164352.0, 163840.0, -163840.0);
	vec4 f1 = fract(x.xxxx+n3);
	vec4 f2 = fract(x.yyyy+n3);
	f1=f1*f1*(3.0-2.0*f1);
	f2=f2*f2*(3.0-2.0*f2);
	vec4 n1 = vec4(0,1.0,157.0,158.0);
	vec4 n2 = vec4(113.0,114.0,270.0,271.0);	
	vec4 vs1 = mix(hash4(p1), hash4(n1.yyyy+p1), f1);
	vec4 vs2 = mix(hash4(n1.zzzz+p1), hash4(n1.wwww+p1), f1);
	vec4 vs3 = mix(hash4(p2), hash4(n1.yyyy+p2), f1);
	vec4 vs4 = mix(hash4(n1.zzzz+p2), hash4(n1.wwww+p2), f1);	
	vs1 = mix(vs1, vs2, f2);
	vs3 = mix(vs3, vs4, f2);
	vs2 = mix(hash4(n2.xxxx+p1), hash4(n2.yyyy+p1), f1);
	vs4 = mix(hash4(n2.zzzz+p1), hash4(n2.wwww+p1), f1);
	vs2 = mix(vs2, vs4, f2);
	vs4 = mix(hash4(n2.xxxx+p2), hash4(n2.yyyy+p2), f1);
	vec4 vs5 = mix(hash4(n2.zzzz+p2), hash4(n2.wwww+p2), f1);
	vs4 = mix(vs4, vs5, f2);
	f1 = fract(x.zzzz+n3);
	f2 = fract(x.wwww+n3);
	f1=f1*f1*(3.0-2.0*f1);
	f2=f2*f2*(3.0-2.0*f2);
	vs1 = mix(vs1, vs2, f1);
	vs3 = mix(vs3, vs4, f1);
	vs1 = mix(vs1, vs3, f2);
	float r=dot(vs1,vec4(0.25));
	//r=r*r*(3.0-2.0*r);
	return r*r*(3.0-2.0*r);
}

void main()
{
	vec2 wrappedTexCoord=fract(texCoord0);
    vec3 normal = normalize(Normal);
    vec3 light = (lightPos.w == 0.0) ? normalize(lightPos.xyz) : normalize(lightPos.xyz - FragPos);
    vec3 view = normalize(viewPos - FragPos);
    vec3 reflectVec = reflect(-light, normal);

   //vec3 color = lightAmbient.rgb * materialAmbient.rgb;
	vec3 color=vec3(2.0, 1.8, 0.5);
    float diff = max(dot(normal, light), 0.0);
    //color += lightDiffuse.rgb * materialDiffuse.rgb * diff;

	color*= texture(map0,texCoord0).rgb;
    
  vec3 noiseInput = vec3(texCoord0 * (noiseScale.xy*0.5), (time*.015) * noiseScale.z);
  vec3 noiseInput2 = vec3(texCoord0 * (noiseScale.xy*0.5), (prevTime*.015) * noiseScale.z);
    float noiseValue = mix(noise4q(vec4(noiseInput2, 1.0)),noise4q(vec4(noiseInput, 1.0)),0.5);
    vec3 noiseColor = vec3(noiseValue);
       color *= noiseColor;

    float spec = pow(max(dot(view, reflectVec), 0.0), materialShininess);
   // color += lightSpecular.rgb * materialSpecular.rgb * spec;
    FragColor = vec4(color, 1.0);

	float brightness = dot(FragColor.rgb, vec3(0.2126, 0.7152, 0.0722));
    if(brightness > 0.5)
        BrightColor = vec4(FragColor.rgb, 1.0);
    else
        BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
}
